package com.kobe.algorithm.leetcode.case2;

import java.util.List;

/**
 * @author ：caopengfei
 * @date ：2021/2/22
 */
public class Solution {

    public   ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode listNode = new ListNode(0);
        ListNode currentNode = null;
        ListNode l1CurrentNode = l1;
        ListNode l2CurrentNode = l2;
        int preValue = 0;
        while(l1CurrentNode !=null || l2CurrentNode != null || preValue>0){
            int l1Value = l1CurrentNode !=null?l1CurrentNode.val:0;
            int l2Value = l2CurrentNode !=null?l2CurrentNode.val:0;
            int result = l1Value+l2Value+preValue;
            int value = result%10;
            preValue  =  result/10;
            currentNode.next = new ListNode(value);
            currentNode = currentNode.next;
            l1CurrentNode = l1CurrentNode == null?null:l1CurrentNode.next;
            l2CurrentNode = l2CurrentNode == null?null:l2CurrentNode.next;
        }
        return listNode.next;
    }
    public static void main(String[] args) {
        ListNode list1 = new ListNode(9);
        list1.next = new ListNode(9);
        list1.next.next = new ListNode(9);
        list1.next.next.next = new ListNode(9);
        list1.next.next.next.next= new ListNode(9);
        list1.next.next.next.next.next= new ListNode(9);
        list1.next.next.next.next.next.next= new ListNode(9);
        ListNode list2 = new ListNode(9);
        list2.next = new ListNode(9);
        list2.next.next = new ListNode(9);
        list2.next.next.next = new ListNode(9);
        Solution solution = new Solution();
        solution.addTwoNumbers(list1,list2);
    }
}
